package com.adpython.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.adpython.dao.PageTabsDao;
import com.adpython.dao.UsersDao;
import com.adpython.domain.PageTabs;
import com.adpython.domain.Users;
import com.adpython.utils.StrUtil;

public class UserController extends BaseMultiController {
	
	private UsersDao usersDao;
	private PageTabsDao pageTabsDao;
	
	public ModelAndView index(HttpServletRequest req, HttpServletResponse resp)throws IOException {
		return new ModelAndView("user");
	}
	
	public ModelAndView register(HttpServletRequest req, HttpServletResponse resp){
		Map result_map=new HashMap();
		List<PageTabs> tagList=pageTabsDao.getAll();
		result_map.put("tagList", tagList);
		String name=req.getParameter("name");
		String email=req.getParameter("loginName");
		String password=req.getParameter("password");
		String city=req.getParameter("city");
		String sex=req.getParameter("sex");
		String birthday=req.getParameter("birthday");
		String phone=req.getParameter("phone");
		String qq=req.getParameter("qq");
		String msn=req.getParameter("msn");
		String address=req.getParameter("address");
		if(!StrUtil.empty(name)&&!StrUtil.empty(email)&&!StrUtil.empty(password)){
			StrUtil.md5(password.getBytes());
			Users user=new Users(email,name,password);
			if(!StrUtil.empty(city))
				user.setCity(city);
			if(!StrUtil.empty(sex)){
				if(sex.equals("male")){
					user.setSex(true);
				}else{
					user.setSex(false);
				}
			}
			if(!StrUtil.empty(birthday))
				user.setBirthday(birthday);
			if(!StrUtil.empty(phone))
				user.setPhone(phone);
			if(!StrUtil.empty(qq))
				user.setQq(qq);
			if(!StrUtil.empty(msn))
				user.setMsn(msn);
			if(!StrUtil.empty(address))
				user.setAddress(address);
			usersDao.save(user);
			result_map.put("register", "1");
		}
		return new ModelAndView("register",result_map);
	}
	
	public ModelAndView listUsers(HttpServletRequest req, HttpServletResponse resp){
		Map result_map=new HashMap();
		List<Users> userList=usersDao.getAll();
		List<PageTabs> tagList=pageTabsDao.getAll();
		result_map.put("userList",userList);
		result_map.put("tagList", tagList);
		result_map.put("tagname", "java");
		return new ModelAndView("page",result_map);
	}
	
	public void setUsersDao(UsersDao usersDao){
		this.usersDao=usersDao;
	}
	public UsersDao getUsersDao(){
		return usersDao;
	}
	
	public void setPageTabsDao(PageTabsDao pageTabsDao){
		this.pageTabsDao=pageTabsDao;
	}
	public PageTabsDao getPageTabsDao(){
		return this.pageTabsDao;
	}
}
